//
//  AddDeviceS3WifiController.m
//  O2Cat
//
//  Created by xiabin on 15/12/17.
//  Copyright © 2015年 QiDao.Purifier. All rights reserved.
//

#import "AddDeviceS3WifiController.h"
#import "AddDeviceS4LoadingController.h"
#import "GizWifiManager.h"

@interface AddDeviceS3WifiController ()

@property (nonatomic,strong) UITextField* wifiNameField;
@property (nonatomic,strong) UITextField* wifiPwdField;
@property (nonatomic,strong) UIButton* switchBtn;
@property (nonatomic,strong) UIButton* nextStepBtn;
@property (nonatomic,strong) UIButton* preStepBtn;

@end

@implementation AddDeviceS3WifiController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = QGPageBG;
    [self.view addSubview:self.wifiNameField];
    [self.view addSubview:self.wifiPwdField];
    [self.view addSubview:self.switchBtn];
    [self.view addSubview:self.nextStepBtn];
    [self.view addSubview:self.preStepBtn];
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdit)]];
    [self layoutSubViews];
    // Do any additional setup after loading the view.
}

- (void) viewWillAppear:(BOOL)animated {
    [self setupNavStyle];
    self.wifiNameField.text = GizWifiManagerIns.wifiName;
    self.wifiPwdField.text = @"12345asdfg";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void) endEdit {
    [self.wifiNameField endEditing:NO];
    [self.wifiPwdField endEditing:NO];
}

- (void) setupNavStyle {
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE_WIDTH, 20)];
    statusBarView.backgroundColor=QGNavBG;
    [self.view addSubview:statusBarView];
    self.navigationController.navigationBar.backgroundColor = QGNavBG;
    self.navigationController.navigationBar.barTintColor = QGNavBG;
    self.navigationItem.title = @"连接WIFI";
    
    
    UIButton* backBtn = [Utils NavButtonStyleNormalBLeft:YES NormalImg:@"back" AndHightLight:@"back"];
    [backBtn addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

-(void) popBack {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) onBtnNextStep {
    GizWifiManagerIns.wifiPwd = self.wifiPwdField.text;
    AddDeviceS4LoadingController* step4 = [[AddDeviceS4LoadingController alloc] init];
    [self.navigationController pushViewController:step4 animated:YES];
}

-(void) onSwitchWifi {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];
}

-(void) layoutSubViews {
    //顶部排布
    __weak __typeof(&*self)weakSelf = self;
    
    [self.wifiNameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view.mas_top).offset(10+NavH);
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(0);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(SCREEN_SIZE_WIDTH);
    }];
    
    [self.wifiPwdField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.wifiNameField.mas_bottom).offset(10);
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(0);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(SCREEN_SIZE_WIDTH);
    }];
    
    [self.switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.wifiPwdField.mas_bottom).offset(20);
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(20);
    }];
    
    [self.nextStepBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.switchBtn.mas_bottom).offset(40);
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX).offset(0);
        make.width.mas_equalTo(weakSelf.view.mas_width).offset(-40);
        make.height.mas_equalTo(50);
    }];
    
    [self.preStepBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.nextStepBtn.mas_bottom).offset(20);
        make.centerX.mas_equalTo(weakSelf.view.mas_centerX).offset(0);
        make.width.mas_equalTo(weakSelf.view.mas_width).offset(-40);
        make.height.mas_equalTo(50);
    }];
}

-(UIButton*) nextStepBtn {
    if (_nextStepBtn == nil) {
        _nextStepBtn = [[UIButton alloc] init];
        _nextStepBtn.backgroundColor = QGNavBG;
        _nextStepBtn.layer.cornerRadius = 4;
        [_nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextStepBtn setTitleColor:QGTextWhite forState:UIControlStateNormal];
        [_nextStepBtn setTitleColor:QGTextGrayL1 forState:UIControlStateHighlighted];
        [_nextStepBtn addTarget:self action:@selector(onBtnNextStep) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextStepBtn;
}

-(UIButton*) preStepBtn {
    if (_preStepBtn == nil) {
        _preStepBtn = [[UIButton alloc] init];
        _preStepBtn.backgroundColor = QGTextGrayL2;
        _preStepBtn.layer.cornerRadius = 4;
        [_preStepBtn setTitle:@"上一步" forState:UIControlStateNormal];
        [_preStepBtn setTitleColor:QGTextWhite forState:UIControlStateNormal];
        [_preStepBtn setTitleColor:QGTextGrayL1 forState:UIControlStateHighlighted];
        [_preStepBtn addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _preStepBtn;
}


-(UIButton*) switchBtn {
    if (_switchBtn== nil) {
        _switchBtn = [[UIButton alloc] init];
        [_switchBtn setTitle:@"切换WIFI" forState:UIControlStateNormal];
        _switchBtn.titleLabel.font = [UIFont systemFontOfSize:[Utils phoneFontL3]];
        [_switchBtn setTitleColor:QGNavBG forState:UIControlStateNormal];
        [_switchBtn addTarget:self action:@selector(onSwitchWifi) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchBtn;
}

-(UITextField*) wifiNameField {
    if (_wifiNameField == nil) {
        _wifiNameField = [[UITextField alloc] init];
        _wifiNameField.delegate = self;
        _wifiNameField.placeholder = @"wifi名称（自动检测手机连接的WIFI）";
        [_wifiNameField setValue:QGTextGrayL1 forKeyPath:@"_placeholderLabel.textColor"];
        [_wifiNameField setValue:[UIFont systemFontOfSize:[Utils phoneFontL4]] forKeyPath:@"_placeholderLabel.font"];
        _wifiNameField.font = [UIFont systemFontOfSize:[Utils phoneFontL4]];
        _wifiNameField.backgroundColor = [UIColor whiteColor];
        _wifiNameField.returnKeyType = UIReturnKeyDone;
        UILabel* leftView1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 50)];
        leftView1.text = @"名称";
        leftView1.textColor = QGTextGrayL2;
        leftView1.textAlignment = NSTextAlignmentCenter;
        _wifiNameField.leftView = leftView1;
        _wifiNameField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _wifiNameField;
}

-(UITextField*) wifiPwdField {
    if (_wifiPwdField == nil) {
        _wifiPwdField = [[UITextField alloc] init];
        _wifiPwdField.delegate = self;
        _wifiPwdField.placeholder = @"输入动态密码";
        [_wifiPwdField setValue:QGTextGrayL1 forKeyPath:@"_placeholderLabel.textColor"];
        [_wifiPwdField setValue:[UIFont systemFontOfSize:[Utils phoneFontL4]] forKeyPath:@"_placeholderLabel.font"];
        _wifiPwdField.font = [UIFont systemFontOfSize:[Utils phoneFontL4]];
        [_wifiPwdField setBackgroundColor:[UIColor whiteColor]];
        _wifiPwdField.returnKeyType = UIReturnKeyDone;
        UILabel* leftView1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 50)];
        leftView1.text = @"密码";
        leftView1.textColor = QGTextGrayL2;
        leftView1.textAlignment = NSTextAlignmentCenter;
        _wifiPwdField.leftView = leftView1;
        _wifiPwdField.leftViewMode = UITextFieldViewModeAlways;
    }
    return _wifiPwdField;
}


@end
